package com.hanhai.zrb.api.biz.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hanhai.zrb.api.biz.dao.mybatis.IntentionMessageMapper;
import com.hanhai.zrb.api.biz.service.CommonCounterService;
import com.hanhai.zrb.api.biz.service.IntentionMessageService;
import com.hanhai.zrb.api.biz.service.IntentionService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.model.common.CommonCounterKey;
import com.hanhai.zrb.model.common.CommonCounterKey.MainKeyType;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.intention.IntentionMessage;
import com.hanhai.zrb.model.intention.dto.IntentionMessageDto;
import com.hanhai.zrb.model.user.User;

@Service("intentionMessageService")
public class IntentionMessageServiceImpl implements IntentionMessageService {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private IntentionMessageMapper intentionMessageMapper;
	@Resource 
    private IntentionService intentionService;	
	@Resource
	private UserService userService;
	@Resource
	private CommonCounterService commonCounterService;
	
	
	@Override
	public PageInfo findIntentionMessages(IntentionMessageDto intentionMessageDto,int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize, true);
		List<IntentionMessage> intentionMessages = intentionMessageMapper.selectAllByCriteria(intentionMessageDto);
		if(CollectionUtils.isNotEmpty(intentionMessages)){
			buildDetails(intentionMessages);
		}
		PageInfo pageInfo = new PageInfo(intentionMessages);
		return pageInfo;
	}

	@Override
	public boolean saveIntentionMessage(IntentionMessage intentionMessage) {
		return 1==intentionMessageMapper.insertSelective(intentionMessage);
	}

	@Override
	public boolean updateIntentionMessage(IntentionMessage intentionMessage) {
		return 1==intentionMessageMapper.updateByPrimaryKeySelective(intentionMessage);
	}
	
	@Override
	public boolean deleteIntentionMessage(Long id) {
		return 1==intentionMessageMapper.deleteByPrimaryKey(id);
	}

	@Override
	public IntentionMessage getIntentionMessage(Long id) {
		return intentionMessageMapper.selectByPrimaryKey(id);
	}

	/**
	 * app意向留言
	 */
	@Override
	public boolean appLeaveMessage(IntentionMessage intentionMessage) {
		if(saveIntentionMessage(intentionMessage)){
			CommonCounterKey commonCounterKey = new CommonCounterKey();
			commonCounterKey.setMainKey(MainKeyType.INTENTION_MESSAGE_COUNT.name());
			commonCounterKey.setSecondaryKey(String.valueOf(intentionMessage.getIntentionId()));
			if(commonCounterService.addOne(commonCounterKey)){
				return true;
			}
		}
		return false;
	}
	
	private void buildDetails(List<IntentionMessage> intentionMessages) {
		List<Long> iids = Lists.newArrayList();
		List<Long> uids = Lists.newArrayList();
		for (IntentionMessage intentionMessage : intentionMessages) {
			iids.add(intentionMessage.getIntentionId());
			uids.add(intentionMessage.getUserId());
		}
		
		List<Intention> intentions = intentionService.getIntentionList(iids);
		Map<Long, String> intentionMap = Maps.newHashMap();
		for(Intention intention:intentions){
			intentionMap.put(intention.getId(), intention.getIntro());
		}
		
		List<User> userList = userService.findByUid(uids);
		Map<Long,User> userMap = Maps.newHashMap();
		for(User user:userList){
			userMap.put(user.getUid(), user);
		}
		
		for (IntentionMessage intentionMessage : intentionMessages){
			User user = userMap.get(intentionMessage.getUserId());
			
			intentionMessage.setIntro(intentionMap.get(intentionMessage.getIntentionId()));
			intentionMessage.setUmobile(user.getUmobile());
			intentionMessage.setUserName(user.getRealname());
		}
	}
	
	
	@Override
	public boolean batchUpdateIntentionMessage(List<IntentionMessage> intentionMessages){
		for (IntentionMessage intentionMessage : intentionMessages) {
			updateIntentionMessage(intentionMessage);
		}
		return true;
	}
	
}
